/* This file is part of the Pardus Economy Planner.

   The Pardus Economy Planner is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation, either version 3 of
   the License, or (at your option) any later version.

   The Pardus Economy Planner is distributed in the hope that it
   will be useful, but WITHOUT ANY WARRANTY; without even the implied
   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the Pardus Economy Planner.  If not, see
   <http://www.gnu.org/licenses/>.
*/

var BuildingInfo = new Class({
  initialize: function(name, tile, image, upkeep, production)
  {
    this.Name = name;
    this.Tile = tile;
    this.Image = image;
    this.Upkeep = upkeep;
    this.Production = production;

    this.ClassName = "BuildingInfo";
  }
});
BuildingInfo.ClassName = "BuildingInfo";

var Building = new Class({
  initialize: function(id, x, y, name, owner, level)
  {
    this.ID = id;
    this.X = x;
    this.Y = y;
    this.Name = name;
    this.Owner = owner;
    this.SetLevel(level);

    this.ClassName = "Building";
  },
  GetLevel: function(value)
  {
    return this.level;
  },
  SetLevel: function(value)
  {
    if (!$defined(value))
      value = 1;
      
    if ($type(value) == "string")
    {
      value = value.replace(/[^0-9]/g, "");
      if (value == "")
        value = 0;

      else
        value = value.toInt();
    }
    value = value.limit(1, 100);
    this.level = value;
    return value;
  },
  Info: function()
  {
    return Global.BuildingInfoList[this.Name];
  },
  Upkeep: function()
  {
    var result = new Hash();
    var upkeep = this.Info().Upkeep;

    for (var commodity in upkeep)
    {
      if (upkeep.hasOwnProperty(commodity))
        result[commodity] = Math.round(upkeep[commodity] * (1 + 0.4 * (this.GetLevel() - 1)));
    }

    return result;
  },
  Production: function()
  {
    var result = new Hash();
    var production = this.Info().Production;

    for (var commodity in production)
    {
      if (production.hasOwnProperty(commodity))
        result[commodity] = Math.round(production[commodity] * (1 + 0.4 * (this.GetLevel() - 1)));
    }

    return result;
  },
  DisplayName: function()
  {
    return this.Owner + "'s " + this.Name;
  }
});
Building.ClassName = "Building";